﻿using Volo.Abp.DependencyInjection;
using Volo.Abp.Users;

namespace Demo.Abp.Extension;

public class AddHeaderHandler : DelegatingHandler, ITransientDependency
{
    private ICurrentUser _currentUser;
    public AddHeaderHandler(ICurrentUser currentUser)
    {
        _currentUser = currentUser;
    }
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        var headers =request.Headers;
        if (!headers.Contains("UserId"))
        {
            headers.Add("UserId",_currentUser.Id?.ToString());
        }
        return await base.SendAsync(request, cancellationToken);
    }
}
